<?php
namespace App\Libs\MWSFinancesService;
use App\Libs\MWSFinancesService\Model\ListFinancialEventGroupsByNextTokenRequest;
use App\Libs\MWSFinancesService\Model\ListFinancialEventGroupsRequest;
use App\Libs\MWSFinancesService\Model\ListFinancialEventsByNextTokenRequest;
use App\Libs\MWSFinancesService\Model\ListFinancialEventsRequest;
class FinancesMws{
    private static $_instance = null;

    private $_fields = ['SellerId',
                        'MWSAuthToken',
                        'MaxResultsPerPage',
                        'FinancialEventGroupStartedAfter',
                        'FinancialEventGroupStartedBefore',
                        'AmazonOrderId',
                        'FinancialEventGroupId',
                        'PostedAfter',
                        'PostedBefore',
                        'NextToken'];

    public static function getInit(){
        if(!is_object(self::$_instance)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 返回给定日期范围的财务事件组。
     * API ListFinancialEventGroups
     * @param array $con
     * @return array
     */
    public function listFinancialEventGroups($con = array())
    {
        $request = $this->_getRequest(new ListFinancialEventGroupsRequest(),$con);
        if (!($request instanceof ListFinancialEventGroupsRequest)) {
            $request = new ListFinancialEventGroupsRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListFinancialEventGroups';
        return $parameters;
    }

    /**
     * 返回给定日期范围的下一页财务事件组。
     * API ListFinancialEventGroupsByNextToken
     * @param array $con
     * @return array
     */
    public function listFinancialEventGroupsByNextToken($con = array())
    {
        $request = $this->_getRequest(new ListFinancialEventGroupsByNextTokenRequest(),$con);
        if (!$request instanceof ListFinancialEventGroupsByNextTokenRequest) {
            $request = new ListFinancialEventGroupsByNextTokenRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListFinancialEventGroupsByNextToken';
        return $parameters;
    }

    /**
     * 返回给定订单，财务事件组或日期范围的财务事件。
     * API ListFinancialEvents
     * @param array $con
     * @return array
     */
    public function listFinancialEvents($con = array())
    {
        $request = $this->_getRequest(new ListFinancialEventsRequest(),$con);
        if (!($request instanceof ListFinancialEventsRequest)) {
            $request = new ListFinancialEventsRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListFinancialEvents';
        return $parameters;
    }

    /**
     * 返回给定订单，财务事件组或日期范围的下一页财务事件。
     * API ListFinancialEventsByNextToken
     * @param array $con
     * @return array
     */
    public function listFinancialEventsByNextToken($con = array())
    {
        $request = $this->_getRequest(new ListFinancialEventsByNextTokenRequest(),$con);
        if (!$request instanceof ListFinancialEventsByNextTokenRequest) {
            $request = new ListFinancialEventsByNextTokenRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListFinancialEventsByNextToken';
        return $parameters;
    }

    private function _getRequest($request,$con = array()) {
        foreach($con as $k=>$v){
            if($k&&in_array($k,$this->_fields)){
                $setter = "set$k";
                $request->$setter($v);
            }
        }
        return $request;
    }
}